package com.example.my_club2.welcome

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.my_club2.R
import com.example.my_club2.databinding.ActivityCommonWebBinding
import com.therouter.TheRouter
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.therouter.router.Routes

@Route(path = "app/CommonWebActivity")
class CommonWebActivity : AppCompatActivity() {
    private lateinit var binding : ActivityCommonWebBinding
    @Autowired(name = "url")
    lateinit var url:String

    @Autowired(name = "text")
    lateinit var text:String
    var handler = Handler()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCommonWebBinding.inflate(layoutInflater)
        setContentView(binding.root)
        TheRouter.inject(this)
        binding.text = text
        Log.d("aaaaaa", "onCreate: ${url}")
        binding.webView.loadUrl(url)
        binding.webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                binding.progressBar.visibility = View.GONE
            }
            override fun onReceivedError(
                view: WebView?,
                errorCode: Int,
                description: String?,
                failingUrl: String?
            ) {
                super.onReceivedError(view, errorCode, description, failingUrl)
                // 加载出错时也隐藏进度条
                binding.progressBar.visibility = View.GONE
                // 可以在这里显示错误提示
            }
        }
        binding.webView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                // 更新进度条
                binding.progressBar.progress = newProgress
                // 如果进度达到100%，延迟一小段时间后隐藏（可选）
                if (newProgress == 100) {
                    binding.progressBar.postDelayed({
                        binding.progressBar.visibility = View.GONE
                    }, 200)
                }
            }
        }
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setDomStorageEnabled(true); // 启用 DOM 存储
        binding.back.setOnClickListener {
            finish()
        }
    }

    override fun onDestroy() {
        //解决webview的内存泄漏
        if (binding.webView != null) {
            // 先执行清理逻辑
            binding.webView?.apply {
                stopLoading()
                webChromeClient = null
                webViewClient = WebViewClient()
                destroy()
            }
        }
        super.onDestroy()
    }
}